package com.gongpb.langchain4j.demo.manus.agent;

import com.gongpb.langchain4j.demo.manus.tool.Tool;
import com.gongpb.langchain4j.demo.manus.tool.ToolResult;
import com.gongpb.langchain4j.demo.manus.tool.ToolUtil;
import dev.langchain4j.agent.tool.ToolExecutionRequest;
import dev.langchain4j.data.message.SystemMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.chat.request.ChatRequest;
import dev.langchain4j.model.chat.request.ChatRequestParameters;
import dev.langchain4j.model.chat.response.ChatResponse;
import lombok.extern.slf4j.Slf4j;
import opennlp.tools.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

import static ch.qos.logback.classic.spi.ThrowableProxyVO.build;

@Slf4j
public class ToolCallAgent extends ReActAgent{
    private static final String SYSTEM_PROMPT = """
                你是一个全能的AI助手，可以解决用户提出的任何任务，你可以调用工具完成各种复杂的请求。
                你可以进行编程、浏览网页
                You are able to perform the following tools:
                {tools}
                
                You are given the following conversation and a follow up question.
                Provide a response that appropriately completes the follow up question.
                
                Chat History:
                {chat_history}
                Follow Up Input: {query}
                """;

    private static final String STEP_PROMPT = """
                You are given the following conversation and a follow up question.
                Provide a response that appropriately completes the follow up question.
                
                Chat History:
                {chat_history}
                Follow Up Input: {query}
                """;

    private List<ToolExecutionRequest> tools = new ArrayList<>();

    @Override
    public boolean think() {
        if (getCurrentStep() == 1){
            getChatMemory().add(SystemMessage.systemMessage(SYSTEM_PROMPT));
            getChatMemory().add(UserMessage.from(getPrompt()+","+STEP_PROMPT));
        }else {
            getChatMemory().add(UserMessage.userMessage(STEP_PROMPT));
        }

        ChatResponse chatResponse = getChatLanguageModel().doChat(
                ChatRequest.builder()
                        .messages(getChatMemory().messages())
                        .parameters(ChatRequestParameters.builder()
                                .toolSpecifications(getTools().stream().map(Tool::getSpecification).toList())
                                .build())
                .build());

        if (!StringUtil.isEmpty(chatResponse.aiMessage().text())){
            log.info("chatResponse.aiMessage().text()：{}",chatResponse.aiMessage().text());
        }

        if (chatResponse.aiMessage().hasToolExecutionRequests()){
            tools = chatResponse.aiMessage().toolExecutionRequests();
            log.info("chatResponse.aiMessage().hasToolExecutionRequests()：{}",chatResponse.aiMessage().hasToolExecutionRequests());
        }

        return chatResponse.aiMessage().hasToolExecutionRequests();
    }

    @Override
    public ToolResult action() {
        for (ToolExecutionRequest toolExecutionRequest : tools){
            ToolResult toolResult = ToolUtil.runTool(toolExecutionRequest);

        }

        return null;
    }

    @Override
    public ChatLanguageModel getChatLanguageModel() {
        return null;
    }

    @Override
    public ChatMemory getChatMemory() {
        return null;
    }

    @Override
    public String getPrompt() {
        return "";
    }
}
